package org.jeecg.modules.demo.api;


import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.utils.AesCbcUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/api/signature")
public class CreateSignature {
    @PostMapping(value = "/getSignature")
    public Result<?> getSignature() {
        /* 加签算法 */
        String sKey = "9cb9961db4b7078940af85087844f7f5";//32位加密秘钥
        String ivParameter = "1234567890ABCDEF";//16位加密偏移量
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        HashMap<String, Object> params = new HashMap<>();
        if(sysUser != null){
            params.put("userId", sysUser.getUsername());
        }else{
            params.put("userId", "jkAdmin");
        }
        // //TODO获取当前登录用户的ID
        params.put("timeStamp", System.currentTimeMillis() / 1000);//请求发起时间戳 秒
        params.put("nonce", RandomStringUtils.randomNumeric(16));//随机生成16位字符串
        String encryptStr = JSONUtils.toJSONString(params); //转成json字符串
        String signature = AesCbcUtil.encrypt(encryptStr, sKey, ivParameter); //将json串加密
        params.put("signature", signature);
        return Result.ok(params);
    }





}
